package com.cheng.dreams.greatlydreams.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.util.Calendar;

/**
 * Created by cheng on 2017/3/16.
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * <p>
 * ━━━━━━感觉萌萌哒━━━━━━
 */

public class ClockView extends View{
    private Display display=null;
    //屏幕的宽
    private int screemWidth=0;
    //屏幕的高
    private int screemHeight=0;

    Paint p=null;
    Paint pw=null;
    Paint pb=null;
    Paint pr=null;
    Paint pl=null;

    int hour;
    int mini;
    int miss;

    boolean isFirst =true;

    public ClockView(Context context) {
        this(context,null);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        display=((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        screemWidth=display.getWidth();
        screemHeight=display.getHeight();
        initCanvas();
    }

    public void initCanvas() {
        p=new Paint();
        p.setColor(Color.BLUE);
        p.setAntiAlias(true);
        p.setStrokeWidth(2);
        p.setTextSize(26);
        p.setStyle(Paint.Style.STROKE);
        p.setTextAlign(Paint.Align.CENTER);

        pw=new Paint();
        pw.setAntiAlias(true);
        pw.setDither(true);
        pw.setColor(Color.WHITE);
        pw.setStrokeWidth(2);
        pw.setStyle(Paint.Style.FILL);

        pb=new Paint();
        pb.setAntiAlias(true);
        pb.setDither(true);
        pb.setColor(Color.BLACK);
        pb.setStrokeWidth(2);
        pb.setStyle(Paint.Style.STROKE);

        pr=new Paint();
        pr.setAntiAlias(true);
        pr.setDither(true);
        pr.setColor(Color.RED);
        pr.setStrokeWidth(5);
        pr.setStyle(Paint.Style.FILL);

        pl=new Paint();
        pl.setAntiAlias(true);
        pl.setDither(true);
        pl.setColor(Color.BLACK);
        pl.setStrokeWidth(5);
        pl.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if(widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }else{
            width=screemWidth;
        }
        if(heightMode==MeasureSpec.EXACTLY){
            height=heightSize;
        }else{
            height=screemHeight/2;
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置中心点
        canvas.translate(getWidth()/2,getHeight()/2);
        //外圆
        canvas.drawCircle(0,0,400,pb);
        //刻度
        for (int i = 0; i < 60; i++) {
            if(i%5==0){
                canvas.drawLine(0,-400,0,-360,p);
                canvas.drawText((i==0?12:i/5)+"",0,-340,p);
            }else{
                canvas.drawLine(0,-400,0,-380,p);
            }
            canvas.rotate(6);
        }
        getTime();
        //中心圆
        canvas.drawCircle(0,0,40,pr);
        //秒
        canvas.save();
        canvas.rotate(miss);
        canvas.drawLine(0,10,0,-370,pl);
        canvas.restore();
        //分
        canvas.save();
        canvas.rotate(mini);
        canvas.drawLine(0,10,0,-300,pl);
        canvas.restore();
        //时
        canvas.save();
        canvas.rotate(hour);
        canvas.drawLine(0,10,0,-200,pl);
        canvas.restore();
        //发送通知重绘通知
        if(isFirst){
            handler.sendEmptyMessageDelayed(0, 1000);
            isFirst=false;
        }

    }

    /**
     * 获取时间
     */
    public void getTime(){
        Calendar calendar=Calendar.getInstance();  //获取当前时间，作为图标的名字
        int mhour=calendar.get(Calendar.HOUR);
        int mminute=calendar.get(Calendar.MINUTE);
        int msecond=calendar.get(Calendar.SECOND);
        hour=30*mhour;
        mini=6*mminute;
        miss=6*msecond;
    }

    /**
     * 重绘
     */
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
                invalidate();
                this.sendEmptyMessageDelayed(0, 1000);
        }
    };

}
